//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Windows.Forms;

using GuidanceExplorer.Model;

namespace GuidanceExplorer.Controls
{
    public partial class ColumnChooserForm : Form
    {
        List<Column> _columns;
        Dictionary<string, bool> _tempStatus;

        public ColumnChooserForm(ColumnCollection columns)
        {
            DialogResult = DialogResult.Cancel;
            InitializeComponent();
            _columns = new List<Column>(columns.Count);
            _tempStatus = new Dictionary<string, bool>(_columns.Count);
            FieldCollection fields = GuidanceType.GetAllFields();

            foreach (KeyValuePair<string, Column> col in columns)
            {
                _columns.Add(col.Value);
                columnsListBox.Items.Add(col.Value.Caption, col.Value.Visible);
                _tempStatus.Add(col.Value.Caption, col.Value.Visible);

                if (fields.ContainsKey(col.Value.Field.Name))
                {
                    fields.Remove(col.Value.Field.Name);
                }
            }

            foreach (Field field in fields.Values)
            {
                Column column = Column.New();
                column.Source = field.Name;
                column.Caption = field.Caption;
                column.Visible = false;
                column.Width = field.Width;
                column.Field = field;
                column.Position = _columns.Count;

                columns.Add(column.Source, column);
                _columns.Add(column);
                columnsListBox.Items.Add(column.Caption, column.Visible);

                _tempStatus.Add(column.Caption, column.Visible);
            }
        }

        private void columnsListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.Index >= 0 && e.Index < _columns.Count)
            {
                Column col = _columns[e.Index];
                if (e.NewValue == CheckState.Checked)
                {
                    col.Visible = true;
                }
                else if (e.NewValue == CheckState.Unchecked)
                {
                    col.Visible = false;
                }
            }
        }

        public bool HasChanges
        {
            get
            {
                foreach (Column col in _columns)
                {
                    if (col.Visible != _tempStatus[col.Caption])
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            foreach (Column col in _columns)
            {
                col.Visible = _tempStatus[col.Caption];
            }

            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
